﻿using System;
using System.Collections.Generic;
using UnityEngine;
using static RoleController;

public class FSMController
{
    public RoleController role;

    public Dictionary<string, FSMState> dicStates;

    public FSMState state;

    ///获取状态类方法

    public Func<string, FSMState> funcGetFSMState;

    public FSMController(RoleController role, string stateName, Func<string, FSMState> funcGetFSMState)
    {
        dicStates = new Dictionary<string, FSMState>();

        this.role = role;
        this.funcGetFSMState = funcGetFSMState;
        SetState(stateName);
    }

    public void SetState(string v)
    {
        if (state != null)
        {
            state.Exit();
        }

        if (dicStates.ContainsKey(v) == false)
        {
            dicStates[v] = funcGetFSMState.Invoke(v);
            dicStates[v].fsm = this;
        }
        state = dicStates[v];   
        state.Enter();
    }

    public void Update()
    {
        state.Update();
    }
}

